package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.manage.common.utils.BeanCopyUtils;
import com.manage.modules.domain.TBaseStationEquip;
import com.manage.modules.dto.TBaseStationEquipHistorySelectDto;
import com.manage.modules.dto.TBaseStationEquipHistorySelectReturnDto;
import com.manage.modules.dto.TBaseStationEquipSelectReturnDto;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseStationEquipHistoryAddBo;
import com.manage.modules.bo.TBaseStationEquipHistoryQueryBo;
import com.manage.modules.bo.TBaseStationEquipHistoryEditBo;
import com.manage.modules.domain.TBaseStationEquipHistory;
import com.manage.modules.mapper.TBaseStationEquipHistoryMapper;
import com.manage.modules.vo.TBaseStationEquipHistoryVo;
import com.manage.modules.service.ITBaseStationEquipHistoryService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备历史Service业务层处理
 *
 * @author wx
 * @date 2021-08-13
 */
@Service
public class TBaseStationEquipHistoryServiceImpl extends ServiceImpl<TBaseStationEquipHistoryMapper, TBaseStationEquipHistory> implements ITBaseStationEquipHistoryService {

    @Override
    public List<TBaseStationEquipHistorySelectReturnDto> option(TBaseStationEquipHistorySelectDto selectDto) {
        LambdaQueryWrapper<TBaseStationEquipHistory> lqw = Wrappers.lambdaQuery();
        if (selectDto != null) {
            lqw.eq(StrUtil.isNotBlank(selectDto.getStationIdC()), TBaseStationEquipHistory::getStationIdC, selectDto.getStationIdC());
        }
        lqw.select(TBaseStationEquipHistory::getEquipHistoryId, TBaseStationEquipHistory::getEquipName);
        List<TBaseStationEquipHistory> list = list(lqw);
        return BeanCopyUtils.listCopy(list, CopyOptions.create(), TBaseStationEquipHistorySelectReturnDto.class);
    }

    @Override
    public TBaseStationEquipHistoryVo queryById(Long equipHistoryId) {
        TBaseStationEquipHistory db = this.baseMapper.selectById(equipHistoryId);
        return BeanUtil.toBean(db, TBaseStationEquipHistoryVo.class);
    }

    @Override
    public List<TBaseStationEquipHistoryVo> queryList(TBaseStationEquipHistoryQueryBo bo) {
        LambdaQueryWrapper<TBaseStationEquipHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getStationIdC()), TBaseStationEquipHistory::getStationIdC, bo.getStationIdC());
        lqw.like(StrUtil.isNotBlank(bo.getEquipName()), TBaseStationEquipHistory::getEquipName, bo.getEquipName());
        lqw.like(StrUtil.isNotBlank(bo.getInstrumentNo()), TBaseStationEquipHistory::getInstrumentNo, bo.getInstrumentNo());
        lqw.eq(bo.getEquipTypeId() != null, TBaseStationEquipHistory::getEquipTypeId, bo.getEquipTypeId());
        lqw.like(StrUtil.isNotBlank(bo.getEquipModel()), TBaseStationEquipHistory::getEquipModel, bo.getEquipModel());
        lqw.eq(bo.getFactoryId() != null, TBaseStationEquipHistory::getFactoryId, bo.getFactoryId());
        lqw.eq(bo.getEquipState() != null, TBaseStationEquipHistory::getEquipState, bo.getEquipState());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseStationEquipHistoryVo> entity2Vo(Collection<TBaseStationEquipHistory> collection) {
        List<TBaseStationEquipHistoryVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseStationEquipHistoryVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseStationEquipHistory> page = (Page<TBaseStationEquipHistory>) collection;
            Page<TBaseStationEquipHistoryVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseStationEquipHistoryAddBo bo) {
        TBaseStationEquipHistory add = BeanUtil.toBean(bo, TBaseStationEquipHistory.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseStationEquipHistoryEditBo bo) {
        TBaseStationEquipHistory update = BeanUtil.toBean(bo, TBaseStationEquipHistory.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseStationEquipHistory entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
